<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="应用名称">
        <el-input v-model="queryParams.keyword" placeholder="应用名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selections.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="clientName" header-align="center" align="center" label="名称"></el-table-column>
      <el-table-column prop="clientId" header-align="center" align="center" label="APPID"></el-table-column>
      <el-table-column prop="scope" header-align="center" align="center" label="SCOPE">
        <template slot-scope="scope">
          <el-tag size="mini" v-for="item in (scope.row.scope.split(','))" :key="item">{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="authorizedGrantTypes" header-align="center" align="center" label="授权类型">
        <template slot-scope="scope">
          <el-tag size="mini" v-for="item in (scope.row.authorizedGrantTypes.split(','))" :key="item">{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="350" prop="webServerRedirectUri" header-align="center" align="center" label="回调地址">
        <template slot-scope="scope">
          <el-tag size="mini" v-for="item in (scope.row.webServerRedirectUri.split(','))" :key="item">{{ item }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="accessTokenValidity" header-align="center" align="center" label="令牌期限"></el-table-column>
      <el-table-column prop="refreshTokenValidity" header-align="center" align="center" label="刷新令牌期限"></el-table-column>
      <el-table-column prop="autoapprove" header-align="center" align="center" label="自动授权">
        <template slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.autoapprove === 'true'"></i>
          <i class="el-icon-error" v-else></i>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="220" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="small" @click="handleUpdate(scope.row.clientId)">修改</el-button>
          <el-button type="text" icon="el-icon-refresh" size="small" @click="resetSecret(scope.row)">重置secret</el-button>
          <el-button type="text" icon="el-icon-delete" size="small" @click="handleDelete(scope.row.clientId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />

    <client-form ref="dataForm" @ok="loadData"></client-form>
  </div>
</template>

<script>
import {deleteClient, findClient, resetClientSecret} from "@/api/auth/client";
import ClientForm from "@/views/auth/client/ClientForm.vue";

export default {
  components: {ClientForm},
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      dataList: [],
      total: 0,
      loading: false,
      selections: [],
      addOrUpdateVisible: false
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    // 获取数据列表
    loadData() {
      this.loading = true
      findClient(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records
          this.total = res.data.total
        }
        this.loading = false
      })
    },

    handleAdd() {
      this.$refs.dataForm.add()
    },

    resetSecret(row) {
      let that = this
      this.$alert(`您正在重置${row.clientName}的密钥，此操作会导致应用无法获取授权，请在此操作完成后立即将对应客户端密钥更换为新生成的密钥，确定要进行此操作吗`, '提示', {
        confirmButtonText: '确定',
        callback: action => {
          that.doResetSecret(row.clientId)
        }
      });
    },

    doResetSecret(clientId) {
      resetClientSecret(clientId).then(res => {
        if (res.success) {
          this.$alert(`新的Secret为:${res.message},请妥善保管`, '重置成功', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      })
    },

    handleUpdate(id) {
      this.$refs.dataForm.edit(id)
    },

    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      }
      this.loadData()
    },

    handleDelete(id) {
      let ids = id ? [id] : this.selections.map(item => {
        return item.id
      })
      this.$confirm('确认删除选中数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteClient(ids).then(res => {
          if (res.success) {
            this.loadData()
            this.$message.success('删除成功')
          }
        })
      })
    }
  }
}
</script>
